package com.hong.spring.config;


import com.sproutsocial.nsq.Publisher;
import lombok.extern.log4j.Log4j2;

/**
 * @author: csh
 * @Date: 2021/4/27 18:06
 * @Description:tubemq配置
 */
@Log4j2
public class MsqMqProducer {


    /**服务端 */
    private static Publisher pushlisher;
    /**服务地址 */
    private String url;


    public MsqMqProducer(String url) {
        this.url = url;
    }

    public void init ()  {

        pushlisher = new Publisher(url);
    }

    /**
     *
     * 功能描述: 发送消息
     *
     * @param:
     * @return:
     * @auther: csh
     * @date: 2021/4/28 17:05
     */
    public Boolean send(String msg,String topic)  {
        try {
            pushlisher.publishBuffered(topic,msg.getBytes());
            return true;
        }catch (Exception e){
            log.error("发送失败{}",e);
        }
        return false;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    /**
     *
     * 功能描述: 关闭
     *
     * @param:
     * @return:
     * @auther: csh
     * @date: 2021/5/18 14:50
     */
    public void close(){
        try {
            if(null!= pushlisher){
                pushlisher.stop();
            }
        }catch (Exception e){
            log.error("关闭失败{}",e);
        } catch (Throwable throwable) {
            log.error("关闭失败{}",throwable);
            throwable.printStackTrace();
        }

    }
}
